package org.jsola.hr.service.excel;

import com.alibaba.excel.context.AnalysisContext;
import lombok.extern.slf4j.Slf4j;
import org.jsola.common.BeanKit;
import org.jsola.common.StrKit;
import org.jsola.exception.ParamException;
import org.jsola.hr.dto.CompanyAddDTO;
import org.jsola.hr.dto.excel.CompanyImportDTO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.vo.CompanyImportExcelVO;
import org.jsola.hr.vo.CompanyImportRecordVO;
import org.jsola.hr.vo.CompanyVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WU
 */
@Slf4j
public class CompanyImportListener extends BaseAnalysisEventListener<CompanyImportDTO> {

    /**
     * List最大盛装数据条数
     */
    private static final Integer MAX_COUNT = 2000;

    /**
     * true忽略错误的行，false有错误中断上传
     */
    protected Boolean force = true;

    /**
     * 导入后的返回结果
     */
    protected CompanyImportExcelVO companyImportExcelVO;

    @Override
    public void invoke(CompanyImportDTO companyImportDTO, AnalysisContext context) {
        // 当前行号
        int row = context.readRowHolder().getRowIndex();
        companyImportDTO.setId(row + 1 + "");
        dataList.add(companyImportDTO);
        if (dataList.size() >= MAX_COUNT) {
            // 入库
            saveData();
            // 存储完成清理 list
            dataList.clear();
        }
    }

    /**
     * 入库
     */
    @Override
    public void saveData() {
        log.info("{}条数据，开始存储数据库！", dataList.size());
        if (dataList.size() == 0) {
            log.warn("没有数据需要入库");
            throw new ParamException("导入的数据为空，请检查模板中是否有内容");
        }
        companyImportExcelVO = new CompanyImportExcelVO();
        companyImportExcelVO.setImportAllList(dataList);
        // 成功的数据
        List<CompanyImportRecordVO> importSucList = new ArrayList<>();
        // 失败的数据
        List<CompanyImportRecordVO> importFailList = new ArrayList<>();
        for (CompanyImportDTO companyImportDTO : dataList) {
            // 用工单位名称去空格
            companyImportDTO.setName(companyImportDTO.getName().trim());
            CompanyAddDTO companyAddDTO = companyImportDTO.to(CompanyAddDTO.class);
            HrException.notNull(tokenUser, "没有设置当前登录用户");
            ICompanyService companyService = BeanKit.getBean(ICompanyService.class);
            // 新增用工单位
            CompanyVO companyVO = companyService.saveNoException(companyAddDTO, tokenUser);
            if (StrKit.isNotEmpty(companyVO.getId())) {
                importSucList.add(companyVO.to(CompanyImportRecordVO.class));
                successCount++;
            } else {
                CompanyImportRecordVO companyImportRecordVO = companyAddDTO.to(CompanyImportRecordVO.class);
                companyImportRecordVO.setReason(companyVO.getName());
                companyImportRecordVO.setId(companyImportDTO.getId());
                importFailList.add(companyImportRecordVO);
            }
        }
        companyImportExcelVO.setImportSucList(importSucList);
        companyImportExcelVO.setImportFailList(importFailList);
        log.info("存储数据库成功！");
    }

    public CompanyImportExcelVO getCompanyImportExcelVO(){
        return companyImportExcelVO;
    }
}
